cap log close 
log using "${logpath}litigation_patlevel.log", replace

/*******************************************************************************
litigation_patlevel

Conducts analysis of litigation at the patient panel
*******************************************************************************/

clear all

qui {
	use "${cleandatapath}patientlevel.dta", clear
		
	gen civ_date=mofd(civ_treatmentdate)
	gen post_civ=t>=civ_date & !missing(civ_date)
	gen crim_date=mofd(crim_treatmentdate)
	gen post_crim=t>=mofd(crim_date) & !missing(crim_date)
	gen lit_date=min(civ_date,crim_date)
	gen post_lit=max(post_civ,post_crim)

	bysort usrds_id: gen pat_flag=_n==1
}

foreach l in civ crim {
	qui {
		bysort usrds_id post_`l': egen totalrides_pre_`l'=sum(NEdialysisrides) if post_`l'==0
		bysort usrds_id: egen pre_ride_tot_`l'=max(totalrides_pre_`l')
		gen freqride_`l'=(pre_ride_tot_`l'>=100 & !missing(pre_ride_tot_`l'))
		
		gen postwindow_`l'=0 if t>=`l'_date-24 & t<`l'_date
		replace postwindow_`l'=1 if  t<`l'_date+24 & t>=`l'_date
		bysort usrds_id dist_code postwindow_`l': egen totalrides_prewindow_`l'=sum(NEdialysisrides) if postwindow_`l'==0
		bysort usrds_id dist_code postwindow_`l': egen totalrides_postwindow_`l'=sum(NEdialysisrides) if postwindow_`l'==1
		bysort usrds_id dist_code: egen pre_totalrides_`l'=max(totalrides_prewindow_`l') if !missing(postwindow_`l')
		bysort usrds_id dist_code: egen post_totalrides_`l'=max(totalrides_postwindow_`l') if !missing(postwindow_`l')
		bysort usrds_id dist_code postwindow_`l': egen totalridemo_prewindow_`l'=sum(any_ride) if postwindow_`l'==0
		bysort usrds_id dist_code postwindow_`l': egen totalridemo_postwindow_`l'=sum(any_ride) if postwindow_`l'==1
		bysort usrds_id dist_code: egen pre_totalridemo_`l'=max(totalridemo_prewindow_`l') if !missing(postwindow_`l')
		bysort usrds_id dist_code: egen post_totalridemo_`l'=max(totalridemo_postwindow_`l') if !missing(postwindow_`l')
		
	}
	
	*Figures A14 and A15, Panel (a)
	twoway (hist pre_totalrides_`l' if pre_totalridemo_`l'>0, ///
			width(20) start(-0.00001)) ///
		(hist post_totalrides_`l' if post_totalridemo_`l'>0, ///
			width(20) start(-0.00001) fcolor(none) lcolor(black)) if pat_flag==1, ///
		xti("") yti(Empirical Probability Density) ///
		graphregion(color(white)) bgcolor(white) ///
		leg(subti("Total Rides in the...") order(1 "Pre-Period" 2 "Post-Period"))
	if "`l'"=="civ" {
		qui graph export "${outpath}Figure_A14a.pdf", as(pdf) replace
	}
	if "`l'"=="crim" {
		qui graph export "${outpath}Figure_A15a.pdf", as(pdf) replace
	}
	
	*Figures A14 and A15, Panel (b)
	twoway (hist pre_totalridemo_`l' if pre_totalridemo_`l'>0, ///
			width(0.99999) start(0.99999)) ///
		(hist post_totalridemo_`l' if post_totalridemo_`l'>0, ///
			width(0.99999) start(0.99999) fcolor(none) lcolor(black)) if pat_flag==1, ///
		xti("") yti(Empirical Probability Mass) ///
		graphregion(color(white)) bgcolor(white) ///
		leg(subti("Total Months Riding in the...") order(1 "Pre-Period" 2 "Post-Period"))
	if "`l'"=="civ" {
		qui graph export "${outpath}Figure_A14b.pdf", as(pdf) replace
	}
	if "`l'"=="crim" {
		qui graph export "${outpath}Figure_A15b.pdf", as(pdf) replace
	}
	
}

qui {
	xtset usrds_id t
	gen rides_nextmonth = (f.any_ride==1)
	gen rider_ridesnext = rides_nextmonth if any_ride==1
	
	gen DVA=newchain==1 if !missing(newchain)
	gen FMC=newchain==2 if !missing(newchain)
	gen IND=newchain==3 if !missing(newchain)
	gen OTH=newchain==4 if !missing(newchain)

	*Patient Characteristics
	label var pat_age "\hspace*{0.5cm} Age (Years)"
	label var dial_tenure "\hspace*{0.5cm} Months with ESRD"
	label var black "\hspace*{0.5cm} Black"
	label var male "\hspace*{0.5cm} Male"
	label var inc_diabetes "\hspace*{0.5cm} Diabetic"
	label var inc_drug "\hspace*{0.5cm} Drug User"
	label var inc_smoker "\hspace*{0.5cm} Smoker"
	label var inc_drinker "\hspace*{0.5cm} Drinker"
	label var inc_noins "\hspace*{0.5cm} Uninsured at Incidence"
	label var inc_empcur "\hspace*{0.5cm} Employed at Incidence"
	*Facility Characteristics
	label var fac_age "\hspace*{0.5cm} Facility Age (Years)"
	label var fac_free "\hspace*{0.5cm} Freestanding Facility"
	label var DVA "\hspace*{1cm} DaVita"
	label var FMC "\hspace*{1cm} Fresenius"
	label var OTH "\hspace*{1cm} Other Chain"
	label var IND "\hspace*{1cm} Independent"
	*Health Outcomes
	label var dialsess "\hspace*{0.5cm} Dialysis Sessions"
	label var hosp_any "\hspace*{0.5cm} All-Cause Hosp."
	label var hosp_fluid1 "\hspace*{0.5cm} Fluid Hosp."
	label var death "\hspace*{0.5cm} Mortality"

	*Table A9
		eststo clear
		eststo: estpost sum pat_age dial_tenure black male inc_diabetes inc_drug inc_smoker inc_drinker inc_noins inc_empcur fac_age fac_free DVA FMC OTH IND dialsess hosp_any hosp_fluid1 death if t<552 & priorauth_date!=0
		eststo: estpost sum pat_age dial_tenure black male inc_diabetes inc_drug inc_smoker inc_drinker inc_noins inc_empcur fac_age fac_free DVA FMC OTH IND dialsess hosp_any hosp_fluid1 death if t<552 & !missing(civ_date)
		eststo: estpost sum pat_age dial_tenure black male inc_diabetes inc_drug inc_smoker inc_drinker inc_noins inc_empcur fac_age fac_free DVA FMC OTH IND dialsess hosp_any hosp_fluid1 death if t<552 & !missing(crim_date)
		eststo: estpost sum pat_age dial_tenure black male inc_diabetes inc_drug inc_smoker inc_drinker inc_noins inc_empcur fac_age fac_free DVA FMC OTH IND dialsess hosp_any hosp_fluid1 death if t<552 & priorauth_date==0 & missing(crim_date) & missing(civ_date)
		eststo: estpost sum pat_age dial_tenure black male inc_diabetes inc_drug inc_smoker inc_drinker inc_noins inc_empcur fac_age fac_free DVA FMC OTH IND dialsess hosp_any hosp_fluid1 death if t<552
		esttab using ${outpath}Table_A9.tex, replace ///
			cells(mean(fmt(%4.2fc %4.2fc %4.3fc %4.3fc %4.3fc ///
				%4.3fc %4.3fc %4.3fc %4.3fc %4.3fc ///
				%4.2fc %4.3fc %4.3fc %4.3fc %4.3fc %4.3fc ///
				%4.2fc %4.3fc %4.3fc %4.3fc))) ///
			label booktabs nonum frag collabels(none) ///
			mtitles("Prior Auth." "Civil" "Criminal" "None" "Overall") ///
			mgroups("Treatment Exposed To", pattern(1 0 0 0 1) prefix(\multicolumn{@span}{c}{) ///
			suffix(}) span erepeat(\cmidrule(lr){@span})) ///
			refcat(pat_age "\textbf{Patient Characteristics}" ///
				   NEdialysisrides "\textbf{Ridership}" ///
				   fac_age "\textbf{Facility Characteristics}" ///
				   DVA "\hspace*{0.5cm} \textit{Chain Affiliation}" ///
				   dialsess "\textbf{Health Outcomes}", ///
				   nolabel) stats(N, label("Patient-Months") fmt(%16.0fc))
}
			   
*Effect of litigation
foreach l in civ crim {
	qui {
		gen months_since_`l'=t-`l'_date if `l'_date>=528 & `l'_date<=684
		su months_since_civ
		replace months_since_`l'=. if months_since_`l'<-12 | months_since_`l'>=12
		su months_since_civ
		replace months_since_`l'=-50 if missing(`l'_date)
		su months_since_civ
		replace months_since_`l'=months_since_`l'+100
		su months_since_civ
		gen post_together_`l'=months_since_`l'
		replace post_together_`l'=100 if months_since_`l'>=100 & !missing(months_since_`l')
		
		if "`l'"=="civ" {
			local other_treat = "i.post_crim"
			label define PA 100 "Civil Enforcement", replace
			local xti = "Months Since Civil Enforcement"
		}
		if "`l'"=="crim" {
			local other_treat = "i.post_civ"
			label define PA 100 "Criminal Enforcement", replace
			local xti = "Months Since Criminal Enforcement"
		}
		
		/*Getting estimates for Tables A26-31
		local i = 1
		foreach var in dialsess death hosp_any hosp_fluid1 rider_death rider_hosp rider_ridesnext {

			estimates clear
			eststo est1: reghdfe `var' ib99.post_together_`l' $pat_controls $fac_controls i.prior_auth `other_treat', a(t newprovusrd dist_code) vce(cluster dist_code)
			eststo est1,addscalars(time_FE 1)
			eststo est1,addscalars(dist_FE 1)
			eststo est1,addscalars(Cont 1)
			eststo est1,addscalars(Fac_FE 1)
			eststo est1,addscalars(Pat_FE 0)
			estadd ysumm
			estimates save ${estpath}litigation_`var'_posttogether_`l',replace

			if `i'<=4 {
				estimates clear
				eststo est1: reghdfe `var' ib99.post_together_`l' $pat_controls $fac_controls i.prior_auth `other_treat' if freqride_`l'==1, a(t dist_code newprovusrd) vce(cluster dist_code)
				eststo est1,addscalars(time_FE 1)
				eststo est1,addscalars(dist_FE 1)
				eststo est1,addscalars(Cont 1)
				eststo est1,addscalars(Fac_FE 1)
				eststo est1,addscalars(Pat_FE 0)
				estadd ysumm
				estimates save ${estpath}litigation_`var'_posttogether_freqride_`l',replace
			}
			
			local i = `i'+1
		}*/
			
		*Tables A26 and A27
		estimates clear
		local i = 1
		foreach var in dialsess death hosp_any hosp_fluid1 {
			estimates use ${estpath}litigation_`var'_posttogether_`l'
			estimates store est`i'
			local i = `i'+1
		}

		label var dialsess "Dialysis Sessions"
		label var death "Mortality"
		label var hosp_any "All-Cause Hosp."
		label var hosp_fluid1 "Fluid Hosp."
		label values post_together_`l' PA
		
		if "`l'"=="civ" {
			local name "A26"
		}
		if "`l'"=="crim" {
			local name "A27"
		}
		
		esttab est* using ${outpath}Table_`name'.tex, ///
			star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
			replace keep(100.post_together_`l') label se stats(time_FE dist_FE Cont Fac_FE r2 ymean N, ///
			label("Year-Month FE" "District FE" "Pat/Fac Controls" "Facility FE" ///
			"R-squared" "Dep. Var. Mean" "Observations")) frag	booktabs
		
		*Tables A28 and A29
		estimates clear
		local i = 1
		foreach var in dialsess death hosp_any hosp_fluid1 {
			estimates use ${estpath}litigation_`var'_posttogether_freqride_`l'
			estimates store est`i'
			local i = `i'+1
		}
		
		if "`l'"=="civ" {
			local name "A28"
		}
		if "`l'"=="crim" {
			local name "A29"
		}
		
		esttab est* using ${outpath}Table_`name'.tex, ///
			star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
			replace keep(100.post_together_`l') label se stats(time_FE dist_FE Cont Fac_FE r2 ymean N, ///
			label("Year-Month FE" "District FE" "Pat/Fac Controls" "Facility FE" ///
			"R-squared" "Dep. Var. Mean" "Observations")) frag	booktabs

		*Tables A30 and A31
		estimates clear
		local i = 1
		foreach var in rider_ridesnext rider_hosp rider_death {
			estimates use ${estpath}litigation_`var'_posttogether_`l'
			estimates store est`i'
			local i = `i'+1
		}

		label var rider_ridesnext "Rides Next Month"
		label var rider_hosp "Hospitalizations"
		label var rider_death "Mortality"
		
		if "`l'"=="civ" {
			local name "A30"
		}
		if "`l'"=="crim" {
			local name "A31"
		}
		
		esttab est* using ${outpath}Table_`name'.tex, ///
			star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
			replace keep(100.post_together_`l') label se stats(time_FE dist_FE Cont Fac_FE r2 ymean N, ///
			label("Year-Month FE" "District FE" "Pat/Fac Controls" "Facility FE" ///
			"R-squared" "Dep. Var. Mean" "Observations")) frag	booktabs	

	}
	
	label var rider_ridesnext "Rides Next Month"
	label var rider_death "Mortality Rate Among Riders"
	label var rider_hosp "Hospitalization Rate Among Riders"

	*Figures A16 and A17
	foreach var in rider_death rider_hosp rider_ridesnext {
		local varlabel : variable label `var'
		estimates clear
		*qui eststo: reghdfe `var' ib99.months_since_`l' $pat_controls $fac_controls i.prior_auth `other_treat', a(t newprovusrd dist_code) vce(cluster dist_code)
		*qui estimates save ${estpath}litigation_`var'_noparboth_wFE_`l',replace
		qui estimates use ${estpath}litigation_`var'_noparboth_wFE_`l'
		coefplot, recast(connected) ciopts(recast(rcap)) baselevel ///
			keep(*months_since_`l') drop(50*) vert ///
			xline(13, lcolor(gs8) lpattern(dash)) ///
			xti("`xti'") ///
			xlab(1 "-12" 4 "-9" 7 "-6" 10 "-3" 13 "0" 16 "3" 19 "6" 22 "9" 25 "12") ///
			yline(0, lcolor(gs8)) ///
			yti("`varlabel'") ylab(, angle(0)) ///
			graphregion(color(white)) bgcolor(white)
		if "`var'"=="rider_ridesnext" & "`l'"=="civ" {
			qui graph export "${outpath}Figure_A16a.pdf", as(pdf) replace
		}
		if "`var'"=="rider_ridesnext" & "`l'"=="crim" {
			qui graph export "${outpath}Figure_A17a.pdf", as(pdf) replace
		}
		if "`var'"=="rider_hosp" & "`l'"=="civ" {
			qui graph export "${outpath}Figure_A16b.pdf", as(pdf) replace
		}
		if "`var'"=="rider_hosp" & "`l'"=="crim" {
			qui graph export "${outpath}Figure_A17b.pdf", as(pdf) replace
		}
		if "`var'"=="rider_death" & "`l'"=="civ" {
			qui graph export "${outpath}Figure_A16c.pdf", as(pdf) replace
		}
		if "`var'"=="rider_death" & "`l'"=="crim" {
			qui graph export "${outpath}Figure_A17c.pdf", as(pdf) replace
		}
	}
}

log close